<?php
//create new armory request
include 'api.php';

//attempt to lookup character. If it fails, exit the script to prevent further errors. 
try {
	$character = $api->getCharacterApi()->getCharacter($realm, $char, 
		array('guild', 'stats', 'items', 'titles', 'professions', 'talents'));
	
//set attributes into variables
//all field names should be in single quotes for consistency

$online = 1;

//character attributes
//add titles
$name = $character['name'];
//$xml->characterInfo->character->attributes()->prefix . " " . $character['name'] . " " 
//. $xml->characterInfo->character->attributes()->suffix;
$level = $character['level'];
$roundedLevel = roundLevel($level);
$points = $character['achievementPoints'];
$lastLogin = date('Y-m-d',(substr($character['lastModified'],0,10)));
$guildName = $character['guild']['name'];
$race = $character['race'];
$genderid = $character['gender'];
$raceid = $character['race'];
$classid = $character['class'];
$itemLevel = $character['items']['averageItemLevel'];

//put possible races into arrays
include 'raceClass.php';

//set the faction and race by looking through the arrays
if (array_key_exists($race,$horde))
{
$faction = 'Horde';
$race = $horde[$race];
}
else
{
$faction = 'Alliance';
$race = $alliance[$race];
}

//convert class number to name			
$class = $character['class'];				
$class = $class_a[$class];


//build character image general and URL to use on other pages
//e.g. http://us.battle.net/static-render/us/magtheridon/207/65951439-avatar.jpg	
$image = $character['thumbnail'];
$imgUrl = "http://us.battle.net/static-render/us/".str_replace("225", "56", $image);
//replace single quotes to allow armory url to work correctly
$realmUrl = str_replace("'","",$realm);

//item url
//http://us.media.blizzard.com/wow/icons/56
$itemUrl = "<img src='http://us.media.blizzard.com/wow/icons/56/". $icon . ".jpg' alt='Item image'/";


//build image url as a variable to use on other pages
$armoryUrl = "<a target='_blank' href='http://us.battle.net/wow/en/character/" . $realmUrl . "/" . 
$char . "/simple'><img class='char' src='" . $imgUrl . "' height='100' alt='Character image'/></a>";

//$talent1 = $character[talents][0][name];
$characterTalents = $character['talents'];
$talent1 = xml_blank($characterTalents, 0, 'name');
$talent2 = xml_blank($characterTalents, 1, 'name');

/*
profession ids
182 = herbalism
186 = mining
393 = skinning

164 = blacksmithing
165 = leatherworking
171 = alchemy
197 = tailoring
202 = engineering
333 = enchanting
755 = jewelcrafting
773 = inscription

185 = cooking
129 = first aid
*/

//define profession ids that have recipes as array
$recipeProfs = array(1=>164, 2=>165, 3=>171, 4=>197, 5=>202, 6=>333, 7=>755, 8=>773, 9=>185, 10=>129);
//$profsArray = array(164 => 'blacksmithing',165 => 'leatherworking',171 => 'alchemy',197 => 'tailoring',
//202 => 'engineering',333 => 'enchanting',755 => 'jewelcrafting',773 => 'inscription',185 => 'cooking');


//put skills and professions into several arrays
//first professions...
$characterProfs = $character['professions']['primary'];

$profId1 = $characterProfs[0]['id'];
$profId2 = $characterProfs[1]['id'];
//skill name
$prof = array ( 0 => xml_blank($characterProfs, 0, 'name'), 1 =>
		xml_blank($characterProfs, 1, 'name')
			);
//skill level
$profval = array ( 0 => xml_blank($characterProfs, 0, 'rank'), 1 =>
		xml_blank($characterProfs, 1, 'rank')
			);
//max skill level
$profmax = array ( 0 => xml_blank($characterProfs, 0, 'max'), 1 =>
		xml_blank($characterProfs, 1, 'max')
			);
			
//secondary skills
$characterSkills = $character['professions']['secondary'];

$skill  = array (
			0 => xml_blank($characterSkills, 0, 'name'),
			1 => xml_blank($characterSkills, 1, 'name'),
			2 => xml_blank($characterSkills, 2, 'name'),
			3 => xml_blank($characterSkills, 3, 'name')
			);

$skillval  = array (
			0 => xml_blank($characterSkills, 0, 'rank'),
			1 => xml_blank($characterSkills, 1, 'rank'),
			2 => xml_blank($characterSkills, 2, 'rank'),
			3 => xml_blank($characterSkills, 3, 'rank')
			);

$skillmax  = array (
			0 => xml_blank($characterSkills, 0, 'max'),
			1 => xml_blank($characterSkills, 1, 'max'),
			2 => xml_blank($characterSkills, 2, 'max'),
			3 => xml_blank($characterSkills, 3, 'max')
			);


//character stats
//set general var first
$characterStats = $character['stats'];
//bars
$health = $characterStats['health'];
$bartype = $characterStats['powerType'];
$regen_combat = $characterStats['mana5Combat'];
$regen_nocombat = $characterStats['mana5'];
$mana = $characterStats['power'];

//strength
$strength = $characterStats['str'];
//$strength_att = $xml->characterInfo->characterTab->baseStats->strength->attributes()->attack;
//$strength_blk = $xml->characterInfo->characterTab->baseStats->strength->attributes()->block;
//$strength_base = $xml->characterInfo->characterTab->baseStats->strength->attributes()->base;
//$strength_items = $strength-$strength_base;

//agility
$agility = $characterStats['agi'];
//$agility_armor = $xml->characterInfo->characterTab->baseStats->agility->attributes()->armor;
//$agility_att = $xml->characterInfo->characterTab->baseStats->agility->attributes()->attack;
//$agility_base = $xml->characterInfo->characterTab->baseStats->agility->attributes()->base;
//$agility_crit = $xml->characterInfo->characterTab->baseStats->agility->attributes()->critHitPercent;
//$agility_items = $agility-$agility_base;

//stamina
$stamina = $characterStats['sta'];
//$stamina_base = $xml->characterInfo->characterTab->baseStats->stamina->attributes()->base;
//$stamina_health = $xml->characterInfo->characterTab->baseStats->stamina->attributes()->health;
//$stamina_pet = $xml->characterInfo->characterTab->baseStats->stamina->attributes()->petBonus;
//$stamina_items = $stamina-$stamina_base;

//intellect
$intellect = $characterStats['int'];
//$intellect_base = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->base;
//$intellect_crit = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->critHitPercent;
//$intellect_mana = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->mana;
//$intellect_pet = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->petBonus;
//$intellect_items = $intellect-$intellect_base;

//spirit
$spirit = $characterStats['spr'];
//$spirit_base = $xml->characterInfo->characterTab->baseStats->spirit->attributes()->base;
//$spirit_health = $xml->characterInfo->characterTab->baseStats->spirit->attributes()->healthRegen;
//$spirit_mana = $xml->characterInfo->characterTab->baseStats->spirit->attributes()->manaRegen;
//$spirit_items = $spirit-$spirit_base;

//armor
$armor = $characterStats['armor'];
//$armor_base = $xml->characterInfo->characterTab->baseStats->armor->attributes()->base;
//$armor_percent = $xml->characterInfo->characterTab->baseStats->armor->attributes()->percent;
//$armor_dmg_absorb = $health / (1-($armor_percent/100));
//$armor_pet = $xml->characterInfo->characterTab->baseStats->armor->attributes()->petBonus;
//$armor_items = $armor-$armor_base;

//resistances
//$arcane = $xml->characterInfo->characterTab->resistances->arcane->attributes()->value;
//$fire = $xml->characterInfo->characterTab->resistances->fire->attributes()->value;
//$frost = $xml->characterInfo->characterTab->resistances->frost->attributes()->value;
//$holy = $xml->characterInfo->characterTab->resistances->holy->attributes()->value;
//$nature = $xml->characterInfo->characterTab->resistances->nature->attributes()->value;
//$shadow = $xml->characterInfo->characterTab->resistances->shadow->attributes()->value;
//$r_blank = $characterStats['resistance'];

//weapons and hand items
$main_dps = round($characterStats['mainHandDps'],3);
$main_max = $characterStats['mainHandDmgMax'];
$main_min = $characterStats['mainHandDmgMin'];
$main_speed = $characterStats['mainHandSpeed'];

$off_dps = round($characterStats['offHandDps'],3);
$off_max = $characterStats['offHandDmgMax'];
$off_min = $characterStats['offHandDmgMin'];
$off_speed = $characterStats['offHandSpeed'];

$power = $characterStats['attackPower'];
//$power_base = $xml->characterInfo->characterTab->melee->power->attributes()->base;
//$power_items = $power-$power_base;
//$power_dps = $xml->characterInfo->characterTab->melee->power->attributes()->increasedDps;

$hitRating = $characterStats['hitRating'];
//$hitRating_percent = $xml->characterInfo->characterTab->melee->hitRating->attributes()->increasedHitPercent;
//$hitRating_penetration = $xml->characterInfo->characterTab->melee->hitRating->attributes()->penetration;
//$hitRating_red_armor = $xml->characterInfo->characterTab->melee->hitRating->attributes()->reducedArmorPercent;
	
$critchance_rating = $characterStats['critRating'];
$critchance_percent = round($characterStats['crit'],3);
//$critchance_plus_percent = $xml->characterInfo->characterTab->melee->critChance->attributes()->plusPercent;

//$expertise = $xml->characterInfo->characterTab->melee->expertise->attributes()->value;
//$expertise_items = $xml->characterInfo->characterTab->melee->expertise->attributes()->additional;
//$expertise_percent = $xml->characterInfo->characterTab->melee->expertise->attributes()->percent;
//$expertise_rating = $xml->characterInfo->characterTab->melee->expertise->attributes()->rating;

//mastery

$ranged_dps = round($characterStats['rangedDps'],3);
$ranged_max = $characterStats['rangedDmgMax'];
$ranged_min = $characterStats['rangedDmgMin'];
$ranged_speed = $characterStats['rangedSpeed'];
//$ranged_haste = $xml->characterInfo->characterTab->ranged->speed->attributes()->value;
//$ranged_haste_percent = $xml->characterInfo->characterTab->ranged->speed->attributes()->hastePercent;
//$ranged_haste_rating = $xml->characterInfo->characterTab->ranged->speed->attributes()->hasteRating;

$ranged_power = $characterStats['rangedAttackPower'];
//$ranged_power_base = $xml->characterInfo->characterTab->ranged->power->attributes()->base;
//$ranged_power_items = $ranged_power-$ranged_power_base;
//$ranged_power_dps = $xml->characterInfo->characterTab->ranged->power->attributes()->increasedDps;
//$ranged_power_pet_att = $xml->characterInfo->characterTab->ranged->power->attributes()->petAttack;
//$ranged_power_pet_spell = $xml->characterInfo->characterTab->ranged->power->attributes()->petSpell;

//item images
//add a function here to return a blank image
$characterItems = $character['items'];

//rewrite this to include the quality and perhaps redo the whole thing as a bigger function
//inputs to function - type (item/spell), id (in this case including slot + id), additional wowhead options (lvl), display text (in this case the image)

//determine dual wield
if (empty($characterItems['offHand']['icon']))//test for an offhand item to see if we're dual wielding
{$dualWield=False;}
else
{$dualWield=True;}

$itemHeadImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['head']['id']."' rel='lvl=$level'>".img_build($characterItems,'head')."</a>"; 

$itemNeckImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['neck']['id']."' rel='lvl=$level'>".img_build($characterItems,'neck')."</a>";

$itemShoulderImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['shoulder']['id']."' rel='lvl=$level'>".img_build($characterItems,'shoulder')."</a>";

$itemShirtImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['shirt']['id']."' rel='lvl=$level'>".img_build($characterItems,'shirt')."</a>";

$itemChestImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['chest']['id']."' rel='lvl=$level'>".img_build($characterItems,'chest')."</a>";

$itemWaistImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['waist']['id']."' rel='lvl=$level'>".img_build($characterItems,'waist')."</a>";

$itemLegsImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['legs']['id']."' rel='lvl=$level'>".img_build($characterItems,'legs')."</a>";

$itemFeetImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['feet']['id']."' rel='lvl=$level'>".img_build($characterItems,'feet')."</a>";

$itemWristImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['wrist']['id']."' rel='lvl=$level'>".img_build($characterItems,'wrist')."</a>";

$itemHandsImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['hands']['id']."' rel='lvl=$level'>".img_build($characterItems,'hands')."</a>";

$itemFinger1Img = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['finger1']['id']."' rel='lvl=$level'>".img_build($characterItems,'finger1')."</a>";

$itemFinger2Img = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['finger2']['id']."' rel='lvl=$level'>".img_build($characterItems,'finger2')."</a>";

$itemTrinket1Img = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['trinket1']['id']."' rel='lvl=$level'>".img_build($characterItems,'trinket1')."</a>";

$itemTrinket2Img = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['trinket2']['id']."' rel='lvl=$level'>".img_build($characterItems,'trinket2')."</a>";

$itemBackImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['back']['id']."' rel='lvl=$level'>".img_build($characterItems,'back')."</a>";

$itemMainHandImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['mainHand']['id']."' rel='lvl=$level'>".img_build($characterItems,'mainHand')."</a>";

$itemOffHandImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['offHand']['id']."' rel='lvl=$level'>".img_build($characterItems,'offHand')."</a>";

$itemRangedImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['ranged']['id']."' rel='lvl=$level'>".img_build($characterItems,'ranged')."</a>";

$itemTabardImg = "<a target='_blank' class='wowhead' href='http://www.wowhead.com/item=".$characterItems['tabard']['id']."' rel='lvl=$level'>".img_build($characterItems,'tabard')."</a>";

$characterAchievements = $character['achievements'];		
	
	}//end try block
	 
	 
catch(Exception $e) 
	{
	echo "<p>Something went haywire... here's what I think:<br/>";

	$error = $e->getMessage();
	if ($error=='Armory offline.'||$error=='Page not found.')
	{
	echo "The armory is currently unavailable</p>";
	include 'offline.php';
		
	}
	else
	{
	echo 'Caught exception: ', $error . '</p>';
	include 'footer.html';
	exit;

	}
}

		

?>